<template>
  <div class="user-list">
    <a-form layout="inline" class="default-search-form gb-flex-form" :form="form" @submit="handleSearch">
      <a-row :gutter="8">
        <a-col :sm="24" :md="12" :lg="8">
          <a-form-item :colon="false" :label="$t('user_code')">
            <a-input v-decorator="['userCode']" :placeholder="$t('user_code')"/>
          </a-form-item>
        </a-col>
        <a-col :sm="24" :md="12" :lg="8">
          <a-form-item :colon="false" :label="$t('user_name')">
            <a-input v-decorator="['userName']" :placeholder="$t('user_name')"/>
          </a-form-item>
        </a-col>
        <a-col :sm="24" :md="12" :lg="8">
          <a-form-item :colon="false" :label="$t('user_status')">
            <DictSelector dictCode="USER_STATUS" v-decorator="['userStatus']" :placeholder="$t('user_status')"/>
          </a-form-item>
        </a-col>
        <a-col :sm="24" :md="12" :xl="8">
          <a-form-item :colon="false" :label="$t('role')">
            <RoleSelector v-decorator="['roleId']" :placeholder="$t('role')" />
          </a-form-item>
        </a-col>
        <a-col :sm="24" :md="12" :lg="8">
          <a-form-item class="gb-search-button">
            <a-button class="item" v-if="funcs['UserSearch']" type="primary" html-type="submit">
              {{ $t(funcs['UserSearch']) }}
            </a-button>
            <a-button type="primary" class="item" @click="handleReset">
              {{ $t('reset') }}
            </a-button>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

    <div class="table-multiple-operations">
      <UserForm v-if="funcs['UserCreate']" class="item" :text="$t(funcs['UserCreate'])" :callback="handleSearch" url="/infra/user/create" />
      <UserUnlock v-if="funcs['UserUnlock']" class="item" :text="$t(funcs['UserUnlock'])" :rows="selectedRows" :callback="handleSearch" />
    </div>

    <a-table :columns="columns"
      :rowKey="record => record.id"
      :dataSource="data"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      size="small"
      :scroll="{ x: 1000 }"
    >
      <div slot="long_column_ellipsis" slot-scope="text">
        <LongTableColumn width="200">{{ text }}</LongTableColumn>
      </div>

      <div class="table-single-operations" slot="action" slot-scope="text, record">
        <Action :buttonInfo="buttonInfo" @actionWidth="handleActionWidth" :funcs="funcs">
          <template  v-if="funcs['UserUpdate']" slot="UserUpdate">
            <UserForm v-if="funcs['UserUpdate']" class="item" :text="$t(funcs['UserUpdate'])" displayType="LINK" :row="record" :callback="handleSearch"  url="/infra/user/update" />
          </template>
          <template  v-if="funcs['UserAuth']" slot="UserAuth">
            <UserAuth v-if="funcs['UserAuth']" class="item" :text="$t(funcs['UserAuth'])" :row="record" />
          </template>
          <template  v-if="funcs['UserUnlock']" slot="UserUnlock">
            <UserUnlock v-if="funcs['UserUnlock']" class="item" :text="$t(funcs['UserUnlock'])" displayType="LINK" :rows="[record]" :callback="handleSearch" />
          </template>
          <template  v-if="funcs['UserResetPwd']" slot="UserResetPwd">
            <UserResetPwd v-if="funcs['UserResetPwd']" class="item" :row="record"  :text="$t(funcs['UserResetPwd'])"/>
          </template>
          <template v-if="funcs['UserToggle']" slot="UserToggle">
            <UserToggle v-if="funcs['UserToggle']" class="item" :row="record" :callback="handleSearch" />
          </template>
        </Action>
      </div>
    </a-table>
  </div>
</template>
<script>
import axios from 'axios'
import common from '@/global/common.js'

export default {
  data() {
    return {
      form: this.$form.createForm(this, {name: this._uid}),
      data: [],
      loading: true,
      selectedRowKeys: [],
      selectedRows: [],
      pagination: {sortField: 'user_name', sortOrder: 'ascend', ...common.getPagination()},
      actionWidth: 0,
    }
  },
  computed: {
    funcs() {
      return common.getVisibleFunc('UserList')
    },
    columns() {
      const columns = [
        { title: this.$t("user_code"), dataIndex: 'userCode', sorter: true, key: 'user_code', width: 100,fixed: 'left' },
        { title: this.$t("user_name"), dataIndex: 'userName',  defaultSortOrder:'ascend', key: 'user_name', width: 120,fixed: 'left'},
        { title: this.$t("user_status"), dataIndex: 'userStatusDesc', key: 'user_status', width: 100, fixed: 'left'},
        { title: this.$t("user_mail"), dataIndex: 'userMail', key: 'user_mail', scopedSlots: { customRender: 'long_column_ellipsis' }, width: 155},
        { title: this.$t("phone"), dataIndex: 'phone', key: 'phone', width: 120, },
        { title: "所属单位", dataIndex: 'unitDesc', key: 'unit_id',scopedSlots: { customRender: 'long_column_ellipsis' }, width: 100,},
        { title: "所属科室", dataIndex: 'departmentDesc', key: 'department_id',width: 100, },
        { title: "关联医生", dataIndex: 'doctorDesc', key: 'doctor_id',width: 100},
        { title: this.$t("order_no"), dataIndex: 'orderNo', sorter: true, key: 'order_no', width: 80 },
        { title: this.$t("last_lock_time"), dataIndex: 'lastLockTime', sorter: true, key: 'last_lock_time', width: 155, }, 
        { title: this.$t("last_unlock_time"), dataIndex: 'lastUnlockTime', sorter: true, key: 'last_unlock_time', width: 155, }, 
        { title: this.$t("last_toggle_time"), dataIndex: 'lastToggleTime', sorter: true, key: 'last_toggle_time', width: 155, },    
        { title: this.$t("create_date"), dataIndex: 'createTime', sorter: true, key: 'create_time', width: 155, }, 
        { title: this.$t("create_by"), dataIndex: 'createByDesc', key: 'create_by', width: 100, },
        { title: this.$t("update_date"), dataIndex: 'updateTime', sorter: true, key: 'update_time', width: 155, }, 
        { title: this.$t("update_by"), dataIndex: 'updateByDesc', key: 'update_by', width: 100, }, 
        { title: this.$t("action"), key: 'action', scopedSlots: { customRender: 'action' }, width: this.actionWidth, fixed: 'right', }
      ]
      return columns
    },
    buttonInfo() {
      return {
        'UserUpdate': 40,
        'UserAuth': 68,
        'UserUnlock': 40,
        'UserResetPwd': 68,
        'UserToggle': 40,
      }
    },
  },
  mounted() {
    this.fetch();
  },
  methods: {
    handleSearch (e) {
      if(e){
        this.pagination.current = 1;
        e.preventDefault();
      }
      this.fetch()
    },
    handleReset () {
      this.form.resetFields();
      this.handleSearch()
    },
    handleActionWidth (data) {
      this.actionWidth = data
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination.pageSize = pagination.pageSize
      this.pagination.current = pagination.current
      this.pagination.sortField = ''
      this.pagination.sortOrder = ''
      if (sorter.column) {
        this.pagination.sortField = sorter.columnKey
        this.pagination.sortOrder = sorter.order
      }
      this.fetch({
        ...filters,
      });
    },
    fetch (param = {}) {
      this.form.validateFields((err, formValue) => {
        if (err) {
          return;
        }
        this.loading = true
        axios({
          url: '/infra/user/find',
          method: 'get',
          params: {
            pageSize: this.pagination.pageSize,
            pageNum: this.pagination.current,
            sortField: this.pagination.sortField,
            sortOrder: this.pagination.sortOrder,
            ...param,
            ...formValue
          },
        }).then((res) => {
          this.pagination.total = res.data.total;
          this.data = res.data.rows;
        }).catch(() => {
          
        }).finally(() => {
          this.loading = false;
        })
      });
    }
  },
}
</script>
<style>
.user-list .ant-form-item-label {
  width: 120px;
}
</style>